package com.xy.blog.user.service.impl;

import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.service.IBaseService;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import com.xy.blog.user.constants.OrganizationConstants;
import com.xy.blog.user.controller.model.req.query.OrganizationQuery;
import com.xy.blog.user.dao.impl.OrganizationDaoImpl;
import com.xy.blog.user.dao.model.OrganizationDO;
import com.xy.blog.user.exception.constants.OrganizationErrorCode;
import com.xy.blog.user.mapstruct.OrganizationConverter;
import com.xy.blog.user.service.model.OrganizationBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/7
 */
@Service
public class OrganizationServiceImpl implements IBaseService<OrganizationQuery, OrganizationBO> {

    @Autowired
    private OrganizationDaoImpl organizationDaoImpl;

    @Autowired
    private OrganizationConverter organizationConverter;

    @Override
    public PageResp<OrganizationBO> listByPage(PageReq<OrganizationQuery> req) throws BaseException {
        return organizationConverter.do2Bo(organizationDaoImpl.listByPage(req));
    }


    public List<OrganizationBO> listByName(String name) {
        OrganizationDO organizationDO = new OrganizationDO();
        organizationDO.setName(name);
        return organizationConverter.do2Bo(organizationDaoImpl.select(organizationDO));
    }


    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(OrganizationBO item) throws BaseException {
        OrganizationDO query = new OrganizationDO();
        query.setCode(item.getCode());
        if (organizationDaoImpl.selectCount(query) > 0) {
            throw new BaseException(OrganizationErrorCode.ORGANIZATION_EXITS);
        }
        OrganizationDO organizationDO = organizationConverter.bo2Do(item);
        OrganizationDO parent = organizationDaoImpl.getByCode(item.getParent());
        if (parent != null) {
            organizationDO.setLevel(parent.getLevel() + OrganizationConstants.SEPARATOR + item.getCode());
            organizationDO.setLevel(parent.getLevel() + OrganizationConstants.SEPARATOR + item.getCode());

            // 更新父节点是否有叶子节点标识
            parent.setLeaf("false");
            parent.setUpdateTime(LocalDateTime.now());
            organizationDaoImpl.updateById(parent);
        } else {
            organizationDO.setLevel(item.getCode());
        }
        organizationDaoImpl.save(organizationDO);

    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void update(OrganizationBO item) throws BaseException {
        OrganizationDO organizationDO = organizationConverter.bo2Do(item);
        organizationDaoImpl.updateById(organizationDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) throws BaseException {
        OrganizationDO currentItem = organizationDaoImpl.getById(id);
        String leaf = currentItem.getLeaf();
        if (leaf != null) {
            if (!Boolean.getBoolean(leaf)) {
                throw new BaseException(OrganizationErrorCode.ORGANIZATION_CANNOT_BE_DELETED);
            }
        }
        organizationDaoImpl.deleteById(id);
    }

    @Override
    public OrganizationBO getById(Long id) throws BaseException {
        return organizationConverter.do2Bo(organizationDaoImpl.getById(id));
    }

}
